#! /usr/bin/env python

import os
import re
import shutil
import markitdown
from datetime import date
import sys
from rich import print as rprint
from openpyxl import Workbook

from datetime import datetime

sys.path.append(os.path.join(os.path.dirname(__file__), "../lib"))

from inner_util import prepare_dir, get_files

SRC_DIR = sys.argv[1]


def remove_whitespace_except_newlines(text):
    # 使用正则表达式替换除换行符以外的空白字符
    # \s 匹配所有空白字符，包括空格、制表符等
    # [^\S\n] 匹配所有非换行符的空白字符
    cleaned_text = re.sub(r"[^\S\n]+", "", text)
    return cleaned_text


def remove_whitespace(text):
    # 使用正则表达式替换除换行符以外的空白字符
    # \s 匹配所有空白字符，包括空格、制表符等
    # [^\S\n] 匹配所有非换行符的空白字符
    cleaned_text = re.sub(r"[^\S]+", "", text)
    return cleaned_text


# def find_first_date(text):
#     # 正则表达式匹配 xxxx年xx月xx日 格式的日期
#     pattern = r"\d{4}年\d{1,2}月\d{1,2}日"
#     # 搜索第一个匹配的日期
#     match = re.search(pattern, text)
#     if match:
#         return match.group()  # 返回匹配的日期
#     else:
#         return None  # 如果没有找到，返回 None


def find_first_date(text):
    last_mat = ""
    mat = re.search(r"(\d{4})年(\d{1,2})月(\d{1,2})日", text)
    if mat:
        last_mat = mat.groups()
        y, m, d = last_mat
        meeting_date = date(year=int(y), month=int(m), day=int(d))
        return meeting_date.strftime("%Y.%m.%d")
    return last_mat


class DateDoc:
    def __init__(self, file_path):
        self.file_path = file_path
        self.no_white_content = None
        self.multi_lines_content = None
        self.first_date = None

        target_file = os.path.basename(file_path)
        content = self.convert()
        if content:
            self.no_white_content = remove_whitespace(content)
            self.first_date = find_first_date(self.no_white_content)

            print("fr", self.first_date)
            target_file = f"{self.first_date}-{target_file}"

        shutil.copy(file_path, f"dist/{target_file}")

    def convert(self):
        md = markitdown.MarkItDown()
        try:
            res = md.convert(self.file_path)
            return res.text_content
        except Exception as e:
            print("Error", e)
            log_err(f"{self.file_path} {str(e)} 有问题")
            return None

    def parse(self):
        text = self.no_white_content

        if text is None:
            return None

        doc = {
            "title": "",
            "code": "",
            "send_by": "",
            "send_to": "",
            "send_date": "",
            "first_date": find_first_date(self.no_white_content)
            if self.no_white_content
            else "",
        }

        def get_index_num(rows: list, reg_exp: re.Pattern) -> int:
            index = -1
            for i, row in enumerate(rows):
                if reg_exp.match(row):
                    index = i
                    break
            return index

        # Split by line, clean up whitespace
        rows = []
        for v in text.split("\n"):
            if v.strip() != "":
                v = re.sub(r"[\s\t]", "", v)
                v = re.sub(r"[\(（]", "（", v)
                v = re.sub(r"[\)）]", "）", v)
                rows.append(v)

        if len(rows) > 1:
            # Line by line processing
            reg_code = re.compile(r"\S+〔\d{4}〕\d+号")
            reg_title = re.compile(r"\S+[请示|函|通知|报告|意见|批复]$")
            reg_send_to = re.compile(r"\S+[:：]$")
            reg_send_date = re.compile(r"^\d{4}年\d{1,}月\d{1,}日$")

            code_row = get_index_num(rows, reg_code)
            send_to_end_row = get_index_num(rows, reg_send_to)
            title_end_row = get_index_num(rows, reg_title)
            send_date_row = get_index_num(rows, reg_send_date)

            if code_row != -1:
                reg_code_match = re.compile(r"(\S+〔\d{4}〕\d+号)")
                code_mat = reg_code_match.search(rows[code_row])
                if code_mat:
                    doc["code"] = code_mat.group(1)

            if title_end_row != -1 and send_to_end_row != -1:
                doc["send_to"] = "".join(
                    rows[title_end_row + 1 : send_to_end_row + 1]
                ).replace("[:：]", "")
                doc["title"] = "".join(rows[code_row + 1 : title_end_row + 1])

            if send_date_row != -1:
                doc["send_date"] = datetime.strptime(
                    rows[send_date_row], "%Y年%m月%d日"
                ).strftime("%Y-%m-%d")

            if doc["title"] == "":
                doc["title"] = "".join(rows)

        else:
            print("No lines")
            # No line splitting processing
            reg_all = re.compile(
                r"((\S+)文件)*(\S+〔\d{4}〕\d+号)*(\S*的(请示|函|通知|报告|意见|批复))([^\s:：]+)[:：]"
            )
            matches = reg_all.search(rows[0])
            if matches:
                doc["send_by"] = matches.group(2) if matches.group(2) else ""
                doc["code"] = matches.group(3) if matches.group(3) else ""
                doc["title"] = matches.group(4) if matches.group(4) else ""
                doc["send_to"] = matches.group(6) if matches.group(6) else ""
            else:
                doc["title"] = text
            if doc["send_by"] == "":
                doc["send_by"] = doc["title"].split("关于")[0]

        return doc


def log_err(err):
    with open("err.log", "a+", encoding="utf-8") as f:
        f.write(err)


def write_file(file_path, content):
    with open(f"dist/{file_path}.txt", "w", encoding="utf-8") as f:
        f.write(content)


def report(docs):
    wb = Workbook()
    ws = wb.active
    # 序号	培训时间	培训施教机构	培训内容	培训类别	培训方式	学时	备注

    ws.append(
        [
            "序号",
            "培训时间",
            "培训施教机构",
            "培训内容",
            "培训类别",
            "培训方式",
            "学时",
            "备注",
        ]
    )

    for d in docs:
        print("d", d)
        ws.append(["", d["first_date"], d["send_by"], d["title"], "", "", "", ""])

    wb.save("dist.xlsx")


def main():
    print("Start")

    prepare_dir("dist")

    error_files = []

    valid_exts = [".docx", ".pdf", ".doc", ".wps"]
    all_files = get_files(SRC_DIR, valid_exts, recursive=True)

    # print("All files", all_files)
    docs = []

    for f in all_files:
        doc = DateDoc(f)
        res = doc.parse()
        print("a", res)
        if res:
            rprint("dddo", res)
            docs.append(res)

    rprint("Error files", error_files)

    docs.sort(key=lambda x: x["first_date"])
    report(docs)
    # write_file(target_file, res.text_content)


if __name__ == "__main__":
    main()
